package com.ruoyi.fjtseac.controller;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.fjtseac.domain.dto.UpdateVideoForm;
import com.ruoyi.fjtseac.domain.dto.VideoDto;
import com.ruoyi.fjtseac.domain.po.Essay;
import com.ruoyi.fjtseac.domain.po.Video;
import com.ruoyi.fjtseac.service.VideoService;
import com.ruoyi.fjtseac.util.LocalFile;
import com.ruoyi.fjtseac.util.aop.PageView;
import com.ruoyi.fjtseac.util.aop.RedisCacheClear;
import com.ruoyi.fjtseac.util.aop.RedisCacheEnable;
import com.ruoyi.fjtseac.util.aop.TableEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;

/**
 * 视频Controller
 *
 * @author icyrain11
 * @date 2023-06-29
 */
@RestController
@RequestMapping("/video")
@Validated
@Api(tags = {"视频接口"})
public class  VideoController {

    @Autowired
    VideoService service;
    @Autowired
    RedisCache redisCache;

    @PreAuthorize("@ss.hasPermi('fjtseac:video:add')")
    @RedisCacheClear
    @ApiOperation("上传视频")
    @PostMapping
    public AjaxResult upload(@Valid VideoDto videoDto, @ApiParam("视频进度唯一标识") @RequestParam String uuid) {
        return service.upload(videoDto, uuid);
    }

    @PreAuthorize("@ss.hasPermi('fjtseac:video:upload')")
    @ApiOperation("文章中插入视频")
    @PostMapping("/essay")
    public AjaxResult upload(@ApiParam(value = "视频文件", required = true) @RequestPart("file") MultipartFile video,
                             @ApiParam("视频进度唯一标识") @RequestParam String uuid) {
        return service.upload(video, uuid);
    }

    @PreAuthorize("@ss.hasPermi('fjtseac:video:remove')")
    @RedisCacheClear
    @ApiOperation("删除视频")
    @DeleteMapping
    public AjaxResult delete(@ApiParam("视频id") @RequestParam @Min(value = 1) Integer videoId) {
        return service.delete(videoId);
    }

    @PreAuthorize("@ss.hasPermi('fjtseac:video:update')")
    @RedisCacheClear
    @ApiOperation("修改视频信息")
    @PutMapping
    public AjaxResult update(@RequestBody UpdateVideoForm updateVideoForm) {
        return service.update(updateVideoForm);
    }

    @RedisCacheEnable
    @GetMapping
    @ApiOperation("分页获取全部视频， 根据视频标题模糊搜索视频")
    public AjaxResult search(@ApiParam(value = "搜索参数") String param,
                             @ApiParam(value = "当前页码", required = true) @Min(value = 1) int pageNum,
                             @ApiParam(value = "每页最大记录数", required = true) @Min(value = 1) int pageSize) {
        return service.search(param, pageNum, pageSize);
    }


    @GetMapping("/progress")
    @ApiOperation("视频上传进度")
    public AjaxResult getProgress(@ApiParam(value = "视频文件的唯一标识", required = true) @NotBlank String uuid) {
        return service.getProgress(uuid);
    }

    @PutMapping("/pv")
    @ApiOperation("浏览量自增接口")
    public AjaxResult incrPv(@ApiParam("视频id") @RequestParam @Min(value = 1) Integer id) {
        Integer pv = redisCache.incr(TableEnum.VIDEO.getTableName() + ":" + id);
        return AjaxResult.success(pv);
    }


    @GetMapping("/pv")
    @ApiOperation("浏览量查询接口")
    public AjaxResult getPv(@RequestParam @Min(value = 1) Integer id) {
        Integer pv = redisCache.getCacheObject(TableEnum.VIDEO.getTableName() + ":" + id);
        if (pv == null) {
            throw new ServiceException("获取浏览量错误");
        }
        return AjaxResult.success(pv);
    }

}

